package com.bianguo.android.tencentpushdemo.adapters;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bianguo.android.tencentpushdemo.R;
import com.bianguo.android.tencentpushdemo.entity.Weather;
import com.bianguo.android.tencentpushdemo.view.widget.SyLinearLayoutManager;
import com.bumptech.glide.Glide;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/7/10.
 */

public class WeatherAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<Weather> weathers;
    private LayoutInflater inflater;

    public WeatherAdapter(Context mContext, ArrayList<Weather> weathers) {
        this.mContext = mContext;
        this.weathers = weathers;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return weathers != null ? weathers.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return weathers.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_weather_layout, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Weather weather = weathers.get(position);
        holder.titleTv.setText(weather.getTitle());
        holder.subTitleTv.setText(weather.getSubTitle());
        holder.contentTv.setText(weather.getContent());
        Glide.with(mContext).load(weather.getImageUrl()).centerCrop().into(holder.image);
//        SyLinearLayoutManager layoutManager = new SyLinearLayoutManager(mContext);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setAutoMeasureEnabled(true);
        holder.replyRecycler.setLayoutManager(layoutManager);
        ArrayList<String> replies = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            replies.add("reply：" + i);
        }
        holder.replyRecycler.setAdapter(new ReplyAdapter(mContext, replies));
        return convertView;
    }

    public static class ViewHolder {

        @BindView(R.id.item_weather_title)
        TextView titleTv;
        @BindView(R.id.item_weather_image)
        ImageView image;
        @BindView(R.id.item_weather_subtitle)
        TextView subTitleTv;
        @BindView(R.id.item_weather_content)
        TextView contentTv;
        @BindView(R.id.item_reply_recycler)
        RecyclerView replyRecycler;

        public ViewHolder(View itemView) {
            ButterKnife.bind(this, itemView);
        }
    }
}
